package com.android.woodpecker;

import java.util.Random;

import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;


public class TripleSum extends FreePlayGameScreen implements OnClickListener {
	Random rand = new Random();
	static int difficulty = 0;
	int[] array = new int[8];
	TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8;
	Button[] option;
	int clickCounter = 0;
	int guess = 0;
	int answer = 0;
	int first, second, third;
	int width, height;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	    WindowManager.LayoutParams.FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		
		Display display = getWindowManager().getDefaultDisplay(); 
	    width = display.getWidth(); 
	    height = display.getHeight(); 
		
		option = new Button[8];

		option[1] = new Button(this);
		option[1].setId(1);
		LayoutParams params = new RelativeLayout.LayoutParams(
				width/4,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
		gameLayout.addView(option[1], params);
		option[1].setOnClickListener(this);

		option[2] = new Button(this);
		option[2].setId(2);
		params = new RelativeLayout.LayoutParams(
				width/4,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.RIGHT_OF, option[1].getId());
		gameLayout.addView(option[2], params);
		option[2].setOnClickListener(this);

		option[4] = new Button(this);
		option[4].setId(4);
		params = new RelativeLayout.LayoutParams(
				width/4,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
		gameLayout.addView(option[4], params);
		option[4].setOnClickListener(this);

		option[3] = new Button(this);
		option[3].setId(3);
		params = new RelativeLayout.LayoutParams(
				width/4,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.LEFT_OF, option[4].getId());
		gameLayout.addView(option[3], params);
		option[3].setOnClickListener(this);

		option[5] = new Button(this);
		option[5].setId(5);
		params = new RelativeLayout.LayoutParams(
				width/4,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.ABOVE, option[1].getId());
		gameLayout.addView(option[5], params);
		option[5].setOnClickListener(this);

		option[6] = new Button(this);
		option[6].setId(6);
		params = new RelativeLayout.LayoutParams(
				width/4,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.RIGHT_OF, option[5].getId());
		params.addRule(RelativeLayout.ABOVE, option[2].getId());
		gameLayout.addView(option[6], params);
		option[6].setOnClickListener(this);

		option[0] = new Button(this);
		option[0].setId(8);
		params = new RelativeLayout.LayoutParams(
				width/4,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.ABOVE, option[4].getId());
		gameLayout.addView(option[0], params);
		option[0].setOnClickListener(this);

		option[7] = new Button(this);
		option[7].setId(7);
		params = new RelativeLayout.LayoutParams(
				width/4,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.LEFT_OF, option[0].getId());
		params.addRule(RelativeLayout.ABOVE, option[3].getId());
		gameLayout.addView(option[7], params);
		option[7].setOnClickListener(this);

		difficulty = (int) (Math.random() * 10 + 1);
		for (int i = 0; i < array.length; i++) {
			array[i] = (int) ((Math.random() * (12 + (difficulty % 4 + 1))) + 1);
			option[i].setText(Integer.toString(array[i]));
		}

		first = (int) ((Math.random() * 7) + 1);
		do {
			second = (int) ((Math.random() * 7) + 1);
		} while (second == first);
		do {
			third = (int) ((Math.random() * 7) + 1);
		} while (first == third || second == third);
		answer = array[first] + array[second] + array[third];

		tv4 = new TextView(this);
		tv4.setId(14);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
		tv4.setText("+");
		tv4.setTextSize(48);
		gameLayout.addView(tv4, params);

		tv3 = new TextView(this);
		tv3.setId(13);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.LEFT_OF, tv4.getId());
		params.rightMargin = 21;
		tv3.setText("?");
		tv3.setTextSize(48);
		gameLayout.addView(tv3, params);

		tv2 = new TextView(this);
		tv2.setId(12);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.LEFT_OF, tv3.getId());
		params.rightMargin = 21;
		tv2.setText("+");
		tv2.setTextSize(48);
		gameLayout.addView(tv2, params);

		tv1 = new TextView(this);
		tv1.setId(11);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.LEFT_OF, tv2.getId());
		params.rightMargin = 21;
		tv1.setText("?");
		tv1.setTextSize(48);
		gameLayout.addView(tv1, params);

		tv5 = new TextView(this);
		tv5.setId(15);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.RIGHT_OF, tv4.getId());
		params.leftMargin = 21;
		tv5.setText("?");
		tv5.setTextSize(48);
		gameLayout.addView(tv5, params);

		tv6 = new TextView(this);
		tv6.setId(16);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.RIGHT_OF, tv5.getId());
		params.leftMargin = 21;
		tv6.setText("=");
		tv6.setTextSize(48);
		gameLayout.addView(tv6, params);

		tv7 = new TextView(this);
		tv7.setId(17);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.RIGHT_OF, tv6.getId());
		params.leftMargin = 21;
		tv7.setText("" + answer);
		tv7.setTextSize(48);
		gameLayout.addView(tv7, params);

		tv8 = new TextView(this);
		tv8.setId(18);
		params = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
		params.addRule(RelativeLayout.ABOVE, tv4.getId());
		params.bottomMargin=21;
		tv8.setTextSize(26);
		gameLayout.addView(tv8, params);

		setContentView(gameLayout);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	public void onClick(View view) {
		clickCounter++;
		Button b = (Button) view;
		int digit = Integer.parseInt(b.getText().toString());
		if (clickCounter % 3 == 1) {
			tv8.setText("");
			guess = digit;
			tv1.setText("" + digit);
		} else if (clickCounter % 3 == 2) {
			if (tv2.getText().toString().equals("x"))
				guess *= digit;
			else
				guess += digit;
			tv3.setText("" + digit);
		} else {
				guess += digit;
			tv5.setText("" + digit);
			if (guess == answer) {
				score = score + (10 + difficulty);
				tv8.setText("You scored: " + (10 + difficulty)+ " points");
			} else {
				score -= 10;
				tv8.setText("WRONG, -10 points");
			}

			// tv8.setText("");
			tv1.setText("?");
			tv3.setText("?");
			tv5.setText("?");
			answer = 0;

			difficulty = (int) (Math.random() * 10 + 1);
			for (int i = 0; i < array.length; i++) {
				if (difficulty > 5)
					array[i] = (int) ((Math.random() * (9 + (difficulty % 4 + 1))) + 1);
				else
					array[i] = (int) ((Math.random() * (12 + (difficulty % 4 + 1))) + 1);
				option[i].setText("" + array[i]);
			}
			int first, second, third;

			first = (int) ((Math.random() * 7) + 1);
			do {
				second = (int) ((Math.random() * 7) + 1);
			} while (second == first);
			do {
				third = (int) ((Math.random() * 7) + 1);
			} while (first == third || second == third);

			if (difficulty > 5) {
				answer = (array[first] * array[second]) + array[third];
				tv2.setText("x");
				tv4.setText("+");
			} else {
				answer = array[first] + array[second] + array[third];
				tv2.setText("+");
				tv4.setText("+");
			}
			tv7.setText("" + answer);
		}

		scoreDisplay.setText("" + score);

	}
}
